YNQ  YNQ-1.5.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups

Macros

#define SY_INTERNALSOCKETPOOL
 
#define SYSocketHandle   NQ_INT
 
#define SYSocketSet   fd_set
 
#define syIsValidSocket(_sock)   (_sock != ERROR)
 
#define syInvalidSocket()   (ERROR)
 
#define syAddSocketToSet(_sock, _set)   FD_SET((_sock), (_set))
 
#define syIsSocketSet(_sock, _set)   FD_ISSET((_sock), (_set))
 
#define syClearSocketSet(_set)   FD_ZERO((_set))
 
#define syClearSocketFromSet(_sock, _set)   FD_CLR((_sock), (_set))
 
#define sySetDatagramSocketOptions(_sock)
 
#define sySetStreamSocketOptions(_sock)
 

Functions

NQ_BOOL syIsSocketAlive (SYSocketHandle sock)
 
NQ_STATUS syShutdownSocket (SYSocketHandle sock)
 
NQ_STATUS syCloseSocket (SYSocketHandle sock)
 
NQ_STATUS syListenSocket (SYSocketHandle sock, NQ_INT backlog)
 
SYSocketHandle syCreateSocket (NQ_BOOL stream, NQ_UINT family)
 
NQ_STATUS syBindSocket (SYSocketHandle sock, const NQ_IPADDRESS *ip, NQ_PORT port, NQ_BOOL reuseAddress)
 
NQ_STATUS syAllowBroadcastsSocket (SYSocketHandle sock)
 
void sySetClientSocketOptions (SYSocketHandle sock)
 
void syGetSocketPortAndIP (SYSocketHandle sock, NQ_IPADDRESS *ip, NQ_PORT *port)
 
NQ_INT sySendToSocket (SYSocketHandle sock, const NQ_BYTE *buf, NQ_COUNT len, const NQ_IPADDRESS *ip, NQ_PORT port)
 
NQ_STATUS syConnectSocket (SYSocketHandle sock, const NQ_IPADDRESS *ip, NQ_PORT port)
 
NQ_INT sySendSocket (SYSocketHandle sock, const NQ_BYTE *buf, NQ_COUNT len)
 
NQ_STATUS sySendSocketAsync (SYSocketHandle sock, const NQ_BYTE *buf, NQ_COUNT len, void(*releaseFunc)(const NQ_BYTE *))
 
NQ_INT sySelectSocket (SYSocketSet *pset, NQ_UINT32 timeout)
 
NQ_INT syRecvFromSocket (SYSocketHandle sock, NQ_BYTE *buf, NQ_COUNT len, NQ_IPADDRESS *ip, NQ_PORT *port)
 
NQ_INT syRecvSocket (SYSocketHandle sock, NQ_BYTE *buf, NQ_COUNT len)
 
NQ_INT syRecvSocketWithTimeout (SYSocketHandle sock, NQ_BYTE *buf, unsigned int len, unsigned int secs)
 
SYSocketHandle syAcceptSocket (SYSocketHandle sock, NQ_IPADDRESS *ip, NQ_PORT *port)
 
NQ_STATUS sySendMulticast (SYSocketHandle socket, const NQ_BYTE *buffer, NQ_COUNT length, const NQ_IPADDRESS *ip, NQ_PORT port)
 
void sySubscribeToMulticast (SYSocketHandle socket, const NQ_IPADDRESS *ip)
 

Detailed Description

Sockets

Most socket operations are BSD 4.x standard calls. However, a few very specific operations are OS-dependent. For a BSD-compliant system use definitions below

Definition of "loopback address". This may be different for different OS. The standard value is 127.0.0.1 for IPv4 and ::1 for IPv6 (in NBO), however, some OS require a value of O.

Macro Definition Documentation

#define SY_INTERNALSOCKETPOOL

Define this parameter to use internal socket pool, comment for per-task pool

#define SYSocketHandle   NQ_INT
#define SYSocketSet   fd_set
#define syIsValidSocket (   _sock)    (_sock != ERROR)
#define syInvalidSocket ( )    (ERROR)
#define syAddSocketToSet (   _sock,
  _set 
)    FD_SET((_sock), (_set))
#define syIsSocketSet (   _sock,
  _set 
)    FD_ISSET((_sock), (_set))
#define syClearSocketSet (   _set)    FD_ZERO((_set))
#define syClearSocketFromSet (   _sock,
  _set 
)    FD_CLR((_sock), (_set))

Remove a socket from a socket set.

Parameters
_sockThe socket to remove.
_set: Socket set.
#define sySetDatagramSocketOptions (   _sock)
#define sySetStreamSocketOptions (   _sock)

Function Documentation

NQ_BOOL syIsSocketAlive ( SYSocketHandle  sock)

Detecting whether a socket is still alive

Parameters
sockSocket id
Returns
This method is said to work on any BSD socket system: issue select() with a zero timeout. on dead socket this should return error instead of zero
NQ_STATUS syShutdownSocket ( SYSocketHandle  sock)

Stop socket operations and disconnect the socket if it was connected

Parameters
sockSocket id
Returns
NQ_SUCCESS or NQ_FAIL
Note
This method is said to work on any BSD socket system
NQ_STATUS syCloseSocket ( SYSocketHandle  sock)

Close socket

Parameters
sockSocket id
Returns
NQ_SUCCESS or NQ_FAIL
NQ_STATUS syListenSocket ( SYSocketHandle  sock,
NQ_INT  backlog 
)

Listen on server socket

Parameters
sockSocket id
backlogMax number of requests in queue
Returns
NQ_SUCCESS or NQ_FAIL
SYSocketHandle syCreateSocket ( NQ_BOOL  stream,
NQ_UINT  family 
)

Create new socket

Parameters
streamTRUE for TCP socket, FALSE for UDP socket
familyCM_IPADDR_IPV4 for IPv4, CM_IPADDR_IPV6 for IPv6
Returns
New socket or invalid socket handle
NQ_STATUS syBindSocket ( SYSocketHandle  sock,
const NQ_IPADDRESS ip,
NQ_PORT  port,
NQ_BOOL  reuseAddress 
)

Bind socket to IP and port

Parameters
sockSocket handle
ipIP to bind to in NBO
portPort to bind to in NBO
reuseAddressWhether to reuse address
Returns
NQ_SUCCESS or NQ_FAIL
NQ_STATUS syAllowBroadcastsSocket ( SYSocketHandle  sock)

Allow broadcasts on an UDP socket

Parameters
sockSocket handle
Returns
NQ_SUCCESS or NQ_FAIL
void sySetClientSocketOptions ( SYSocketHandle  sock)

Tune new client socket

Parameters
sockSocket handle
void syGetSocketPortAndIP ( SYSocketHandle  sock,
NQ_IPADDRESS ip,
NQ_PORT port 
)

Get IP and port the socket is bound to

Parameters
sockSocket handle
ipBuffer for IP address in NBO
portBuffer for port number in NBO
NQ_INT sySendToSocket ( SYSocketHandle  sock,
const NQ_BYTE buf,
NQ_COUNT  len,
const NQ_IPADDRESS ip,
NQ_PORT  port 
)

Send a UDP message to a specific addressee

Parameters
sockSocket handle
bufData to send
lenNumber of bytes to send
ipNumber of bytes to send
portPort number to send to in NBO
Returns
Number of bytes sent or NQ_FAIL
NQ_STATUS syConnectSocket ( SYSocketHandle  sock,
const NQ_IPADDRESS ip,
NQ_PORT  port 
)

Connect to a remote server port

Parameters
sockSocket handle
ipIP address of the server in NBO
portPort number of the server in NBO
Returns
NQ_SUCCESS or NQ_FAIL
NQ_INT sySendSocket ( SYSocketHandle  sock,
const NQ_BYTE buf,
NQ_COUNT  len 
)

Send bytes over a connected socket

Parameters
sockSocket handle
bufData to send
lenNumber of bytes to send
Returns
NQ_SUCCESS or NQ_FAIL
NQ_STATUS sySendSocketAsync ( SYSocketHandle  sock,
const NQ_BYTE buf,
NQ_COUNT  len,
void(*)(const NQ_BYTE *)  releaseFunc 
)

Send bytes asynchronously over a connected socket

Parameters
sockSocket handle
bufData to send
lenNumber of bytes to send
releaseFuncCallback function for releasing the buffer
Returns
NQ_SUCCESS or NQ_FAIL
NQ_INT sySelectSocket ( SYSocketSet pset,
NQ_UINT32  timeout 
)

Select on sockets

Parameters
psetPointer to the file set
timeoutTimeout in seconds
Returns
Number of sockets with data pending, zero on timeout or NQ_FAIL on error
NQ_INT syRecvFromSocket ( SYSocketHandle  sock,
NQ_BYTE buf,
NQ_COUNT  len,
NQ_IPADDRESS ip,
NQ_PORT port 
)

Receive a UDP message

Parameters
sockSocket handle
bufReceive buffer
lenBuffer length
ipBuffer for sender IP address in NBO
portBuffer for sender port in NBO
Returns
Number of bytes received or NQ_FAIL
NQ_INT syRecvSocket ( SYSocketHandle  sock,
NQ_BYTE buf,
NQ_COUNT  len 
)

Receive a UDP message from any sender

Parameters
sockSocket handle
bufReceive buffer
lenBuffer length
Returns
Number of bytes received or NQ_FAIL
NQ_INT syRecvSocketWithTimeout ( SYSocketHandle  sock,
NQ_BYTE buf,
unsigned int  len,
unsigned int  secs 
)

Receive from a datagram or a TCP stream or time out if no data on sockets.

Parameters
sockSocket handle.
bufReceive buffer.
lenBuffer size.
secsNumber of seconds to wait for data on sockets.
Returns
Number of bytes received or NQ_FAIL on error.
SYSocketHandle syAcceptSocket ( SYSocketHandle  sock,
NQ_IPADDRESS ip,
NQ_PORT port 
)

Accept client socket

Parameters
sockServer socket handle
ipBuffer for client IP address in NBO
portBuffer for client port in NBO
Returns
New socket ID or invalid handle
NQ_STATUS sySendMulticast ( SYSocketHandle  socket,
const NQ_BYTE buffer,
NQ_COUNT  length,
const NQ_IPADDRESS ip,
NQ_PORT  port 
)

Send multicast datagram

Parameters
socketSocket handle
bufferData to send
lengthNumber of bytes to send
ipDestination IP
portDestination port
Returns
NQ_FAIL or number of bytes sent
void sySubscribeToMulticast ( SYSocketHandle  socket,
const NQ_IPADDRESS ip 
)

Subscribe IP address to remote socket as a multicast

Parameters
socketRemote socket handle
ipPointer to the IP address to subscribe. This value should be a multicast address in Network Byte Order (NBO).
Note
This function should analyze the IP address type (either IPV4 or IPv6) and builds IP address in the system format (e.g., -sockaddr_in/sockaddr_in6) accordingly before subscribe it.